package com.sunnyatwal.kettlebell.clock.display;

import android.content.Context;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sunnyatwal.kettlebell.R;
import com.sunnyatwal.kettlebell.model.Exercise;
import com.sunnyatwal.kettlebell.model.ExerciseUnit;
import com.sunnyatwal.kettlebell.model.Rest;
import com.sunnyatwal.kettlebell.model.SubExercise;
import com.sunnyatwal.kettlebell.model.Work;

public class ClockDisplay implements KettlebellDisplay {
	
	private TextView mTxtWorkout;
	private TextView mTxtExercise;
	private TextView mTxtWeight;
	private TextView mTxtReps;
	private TextView mTxtRemainingTime;
	private TextView mTxtCurrentTime;
	
	private CharSequence mStrWorkout;
	private CharSequence mStrExercise;
	private CharSequence mStrWeight;
	private CharSequence mStrRemainingTime;
	private CharSequence mStrCurrentTime;	
	private CharSequence mStrReps;	
	
	private ProgressBar mRepBar;
	
	private int mRepBarProgress;
	
	private Context mContext;
	
	
	public ClockDisplay(Context context, TextView workout, TextView exercise, TextView weight, TextView reps, ProgressBar repBar, TextView remainingTime, TextView currentTime) {
		
		mContext = context;
		this.mTxtWorkout = workout;
		this.mTxtExercise = exercise;
		this.mTxtWeight = weight;
		this.mTxtReps = reps;
		this.mRepBar = repBar;
		this.mTxtRemainingTime = remainingTime;
		this.mTxtCurrentTime = currentTime;
	}
	
	
	public void UpdateClockFields(TextView workout, TextView exercise, TextView weight, TextView reps, ProgressBar repBar, TextView remainingTime, TextView currentTime) {
	
		this.mTxtWorkout = workout;
		this.mTxtExercise = exercise;
		this.mTxtWeight = weight;
		this.mTxtReps = reps;
		this.mRepBar = repBar;
		this.mTxtRemainingTime = remainingTime;
		this.mTxtCurrentTime = currentTime;
		
		UpdateFieldsFromLastState();
	}
	
	
	@Override
	public void ToDisplay(Exercise exercise) {
		
		try {
			
			ExerciseUnit u = exercise.getCurrentExercise();
			
			if(exercise.isFinished()) {
				Done();
			}
			else {				
				
				String title = exercise.getTitle();
				String name = u.getName();
				String weight = "";
				String reps = "";				
				String remTime = "";
				
				int repCompleted = 0;
				
				if (u instanceof SubExercise) {
					
					SubExercise s = (SubExercise) u;
					weight = s.getWeightPretty(false);
					reps = s.getRepsPretty(false);	
					repCompleted = s.getCurrentRepCompleted();
					remTime = exercise.getRemainingTimePretty(false);					
				}
				else if(u instanceof Rest || u instanceof Work) {
					
					remTime = exercise.getRemainingTimePretty(false);
				}
								
				mTxtWorkout.setText(title);				
				mTxtRemainingTime.setText(remTime);
				
				mTxtExercise.setText(name);				
				mTxtCurrentTime.setText(u.getRemainingTimePretty(false));				
				
				mTxtWeight.setText(weight);
				mTxtReps.setText(reps);
				mRepBar.setProgress(repCompleted);
				
				SaveCurrentState();
			}			
					
		} catch (Exception e1) {
			e1.printStackTrace();
		}		
	}

	
	@Override
	public void Done() {
		
		Reset();		
		mTxtWorkout.setText(mContext.getString(R.string.string_done));
		SaveCurrentState();
	}

	
	@Override
	public void Reset() {
		
		mTxtWorkout.setText(mContext.getString(R.string.string_ready));
		mTxtRemainingTime.setText("");		
		mTxtExercise.setText("");						
		mTxtCurrentTime.setText(mContext.getString(R.string.string_time_idle));	
		mTxtWeight.setText("");
		mTxtReps.setText("");
		mRepBar.setProgress(0);
		SaveCurrentState();
	}

	
	public void SaveCurrentState() {
		mStrWorkout = mTxtWorkout.getText();
		mStrExercise = mTxtExercise.getText();
		mStrWeight = mTxtWeight.getText();
		mStrReps = mTxtReps.getText();
		mRepBarProgress = mRepBar.getProgress();
		mStrRemainingTime = mTxtRemainingTime.getText();
		mStrCurrentTime = mTxtCurrentTime.getText();
	}
	
	
	public void UpdateFieldsFromLastState() 
	{
		mTxtWorkout.setText(mStrWorkout);
		mTxtRemainingTime.setText(mStrRemainingTime);		
		mTxtExercise.setText(mStrExercise);						
		mTxtCurrentTime.setText(mStrCurrentTime);	
		mTxtWeight.setText(mStrWeight);
		mTxtReps.setText(mStrReps);
		mRepBar.setProgress(mRepBarProgress);
	}
	
	
}
